/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JFrame;
import org.opu.vcforms.actions.i18n.StatementI18N;
import org.opu.vcforms.actions.i18n.StatementI18NImpl;
import org.opu.vcforms.editor.actions.CloseViewEditAction;
import org.opu.vcforms.editor.actions.EditEditAction;

import org.opu.vcforms.editor.actions.EventBusEditAction;
import org.opu.vcforms.editor.actions.PrintEditAction;
import org.opu.vcforms.resource.ResourceHandle;
import org.opu.vcforms.view.component.EditProperties;
import org.opu.vcforms.view.component.GUIProperties;
import org.opu.vcforms.view.component.QuestionDialog;

/**
 * 
 * @author yura
 */
public abstract class EditViewFactory {

    /** Creating frame height */
    private static final int FR_HEIGHT = 440;
    /** Creating frame width */
    private static final int FR_WIDTH = 540;
    /** last editor */
    protected EditView editView;

    protected final TitleI18n ttlI18N;
    protected final StatementI18N stI18N;

    public EditViewFactory(TitleI18n i18n, StatementI18N stI18N) {
        this.ttlI18N = i18n;
        this.stI18N = stI18N;
    }

    public EditViewFactory() {
        this(new TitleI18nImpl(), new StatementI18NImpl());
    }

    public void createView(String text, String title, File file) {
        if (text.length() > GUIProperties.instance().editor.getMaximalFileSize()){
            int question = QuestionDialog.question(stI18N.title(), stI18N.msg(),
                    new String[]{stI18N.btnContinue(), stI18N.btnCancel()},
                    stI18N.open(), stI18N.sysopen(), stI18N.print());

            switch(question){
                case 1:
                    editView = getView(text, title, file);
                    break;
                case 2:
                    new EditEditAction(null).edit(file);
                    break;
                case 3:
                    new PrintEditAction(null).print(file, GUIProperties
                            .instance().editor.getPrintCommand());
                    break;
            }
        } else {
            editView = getView(text, title, file);
        }
    }

    /**
     * Create and show view. Return editorPane on it view
     *
     * @param text
     *            content
     * @param title
     *            title of it
     * @return editorPane on it view with text.
     */
    protected abstract EditView getView(String text, String title, File file);

    /**
     * 	Create default frame
     * @param eventBus for creating toolbar and menu
     * @return
     */
    protected JFrame mkFrame(EventBusEditAction eventBus) {
        final JFrame fr = new JFrame(ResourceHandle.getProperty(
                ResourceHandle.APPLICATION_TITLE) + " - " + ttlI18N.editorTitle());

        fr.setSize(FR_WIDTH, FR_HEIGHT);
        fr.setLocationRelativeTo(null);
        fr.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        final CloseViewEditAction action = (CloseViewEditAction) 
                eventBus.getAction(CloseViewEditAction.MNAME);
        fr.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                action.action(null);
            }
        });

        fr.getContentPane().add(new EditToolbar(eventBus), BorderLayout.NORTH);

        return fr;
    }

    /**
     * Create GUI implemention of TextView
     *
     * @param text
     *            text of it view
     * @return GUI implemention of TextView
     */
    protected TextView mkTextView(String text, File file) {
        return new TextViewImpl(null, text, file);
    }
}
